---
title: "Example: Speech to Text | Voice | Kastrax Docs"
description: Example of using Kastrax to create a speech to text application.
---

import { GithubLink } from '@/components/github-link';

# Smart Voice Memo App ✅

The following code snippets provide example implementations of Speech-to-Text (STT) functionality in a smart voice memo application using Next.js with direct integration of Kastrax. For more details on integrating Kastrax with Next.js, please refer to our [Integrate with Next.js](/docs/frameworks/next-js) documentation.

## Creating an Agent with STT Capabilities ✅

The following example shows how to initialize a voice-enabled agent with OpenAI's STT capabilities:

```typescript filename="src/kastrax/agents/index.ts"
import { openai } from '@ai-sdk/openai';
import { Agent } from '@kastrax/core/agent';
import { OpenAIVoice } from '@kastrax/voice-openai';

const instructions = `
You are an AI note assistant tasked with providing concise, structured summaries of their content... // omitted for brevity
`;

export const noteTakerAgent = new Agent({
  name: 'Note Taker Agent',
  instructions: instructions,
  model: openai('gpt-4o'),
  voice: new OpenAIVoice(), // Add OpenAI voice provider with default configuration
});
```

## Registering the Agent with Kastrax ✅

This snippet demonstrates how to register the STT-enabled agent with your Kastrax instance:

```typescript filename="src/kastrax/index.ts"
import { createLogger } from '@kastrax/core/logger';
import { Kastrax } from '@kastrax/core/kastrax';

import { noteTakerAgent } from './agents';

export const kastrax = new Kastrax({
  agents: { noteTakerAgent }, // Register the note taker agent
  logger: createLogger({
    name: 'Kastrax',
    level: 'info',
  }),
});
```

## Processing Audio for Transcription ✅

The following code shows how to receive audio from a web request and use the agent's STT capabilities to transcribe it:

```typescript filename="app/api/audio/route.ts"
import { kastrax } from '@/src/kastrax'; // Import the Kastrax instance
import { Readable } from 'node:stream';

export async function POST(req: Request) {
  // Get the audio file from the request
  const formData = await req.formData();
  const audioFile = formData.get('audio') as File;
  const arrayBuffer = await audioFile.arrayBuffer();
  const buffer = Buffer.from(arrayBuffer);
  const readable = Readable.from(buffer);

  // Get the note taker agent from the Kastrax instance
  const noteTakerAgent = kastrax.getAgent('noteTakerAgent');
 
  // Transcribe the audio file
  const text = await noteTakerAgent.voice?.listen(readable);

  return new Response(JSON.stringify({ text }), {
    headers: { 'Content-Type': 'application/json' },
  });
}
```

You can view the complete implementation of the Smart Voice Memo App on our GitHub repository.

<br />
<br />
<hr className="dark:border-[#404040] border-gray-300" />
<br />
<br />
<GithubLink
  link={
    "https://github.com/kastrax-ai/voice-examples/tree/main/speech-to-text/voice-memo-app"
  }
/>
